using System;

namespace SilverAmp.Common.DecoupledEvents
{
    public class DecoupledEventSubscription<TPayload>:IDecoupledEventSubscription
    {
        public Action<TPayload> Target { get; set; }
        public SubscriptionToken SubscriptionToken { get; set; }

        public DecoupledEventSubscription( Action<TPayload> target)
        {
            Target = target;
        }
        public virtual Action<object[]> GetExecutionStrategy()
        {
            Action<TPayload> action = this.Target;
            
            if (action != null)
            {
                return arguments =>
                           {
                               TPayload argument = default(TPayload);
                               if (arguments != null && arguments.Length > 0 && arguments[0] != null)
                               {
                                   argument = (TPayload)arguments[0];
                               }
                               action(argument);
                           };
            }
            return null;
        }

    }
}